package com.lw.carmanage.security.service;

import com.lw.carmanage.mapper.SysUserMapper;
import com.lw.carmanage.pojo.SysUser;
import com.lw.carmanage.security.BaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/29 20:52
 */
@Component
public class MyUserService implements UserDetailsService {

    private SysUserMapper userMapper;

    @Autowired
    public void setUserMapper(SysUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userMapper.findByUserName(username);

        if(sysUser==null){
            throw new UsernameNotFoundException("用户名密码不存在");
        }

        if(sysUser.getStatus()==0){
            throw new LockedException("账号被封禁");
        }
        return new BaseUser(username,sysUser.getPassword(),new ArrayList<>(),sysUser.getUserId());
    }
}
